package com.mjk.common.base.scope;


import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import java.util.concurrent.ConcurrentHashMap;

public class BeanRefreshScope implements Scope{

    public static final String SCOPE_REFRESH = "refresh";
    private static final BeanRefreshScope INSTANCE = new BeanRefreshScope();
    
    //来个map用来缓存bean
    private final ConcurrentHashMap<String, Object> beanMap = new ConcurrentHashMap<>();
 
    private BeanRefreshScope() {
    } 
    public static BeanRefreshScope getInstance() {
        return INSTANCE;
    }
    /**
     * 清理当前
     */
    public static void clean() {
        INSTANCE.beanMap.clear();
    }
 
    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        return beanMap.computeIfAbsent(name, k -> objectFactory.getObject());

    }

	@Override
	public Object remove(String name) {		
		return INSTANCE.beanMap.remove(name);
	}

	@Override
	public void registerDestructionCallback(String name, Runnable callback) {
        // Do nothing
	}

	@Override
	public Object resolveContextualObject(String key) {		
		return null;
	}

	@Override
	public String getConversationId() {		
		return null;
	}

}
